<?php

namespace App\Model;

use Illuminate\Support\Facades\DB;

/**
 * Class Commodity_classify
 * @package App\Model
 * @User wuqin
 * @Date 2018/3/14 0014
 * @Time 12:53
 */
class Commodity_classify extends Common
{
    /**
     * @var bool
     */
    public $timestamps = false;
    /**
     * @var string
     */
    protected $primaryKey = 'commodity_classify_id';
    /**
     * @var string
     */
    protected $table = 'commodity_classifys';

    /**
     * 方法 : add_commodity_classify
     * @User wuqin
     * @DateTime 2018-03-14 12:53
     * @param $var
     * @return bool
     */
    public function add_commodity_classify($var)
    {
        $this->classify_name = $var['classify_name'];
        $this->classify_index_img = $var['classify_index_img'];
        $this->parent_id = $var['parent_id'];
        if (!$this->save())
            return false;
        return true;
        $where = array(
            array(
                'where', 'commodity_classify_id', '=', $this->commodity_classify_id
            )
        );
        $data = $this->get($where, '*', 0, 0);
        if (empty($data)) {
//            $this->set_empty_array_redis('commodity_classify:'.$this->commodity_classify_id.':');
            return false;
        }
        $this->set_array($data);
        if (!empty($keys = $this->keys_redis('commodity_classify:' . $this->commodity_classify_id . ':*'))) {
            return true;
        }
        return false;
    }

    /**
     * 方法 : set_array
     * @User wuqin
     * @DateTime 2018-03-29 20:39
     * @param $data
     * @return bool
     */
    public function set_array($data)
    {
        if (empty($data))
            return false;
        foreach ($data as $key => $value) {
            $this->set_array_redis('commodity_classify:' . $value['commodity_classify_id'] . ':parent:' . $value['parent_id'], $value);
        }
    }

    /**
     * 方法 : update_commodity_classify
     * @User wuqin
     * @DateTime 2018-03-14 12:53
     * @param $var
     * @return bool
     */
    public function update_commodity_classify($var)
    {
        $this->classify_name = $var['classify_name'];
        $this->classify_index_img = $var['classify_index_img'];
        $this->parent_id = $var['parent_id'];

        if (!$this->save())
            return false;
        $where = array(
            array(
                'where', 'commodity_classify_id', '=', $this->commodity_classify_id
            )
        );
        $data = $this->get($where, '*', 0, 0);
        if (empty($data)) {
            $this->set_empty_array_redis('commodity_classify:' . $this->commodity_classify_id . ':');
            return false;
        }
        $this->set_array($data);
        if (!empty($keys = $this->keys_redis('commodity_classify:' . $this->commodity_classify_id . ':*'))) {
            return true;
        }
        return false;
    }

    /**
     * 方法 : get_all_classify
     * @User wuqin
     * @DateTime 2018-03-14 12:53
     * @return mixed
     */
    public function get_all_classify()
    {
        $key_index = 'commodity_classify:*';
        if (empty($keys = $this->keys_redis($key_index))) {
            $data = $this->get(array(), '*', 0, 0, ['parent_id', 'asc']);
            if (empty($data))
                $this->set_empty_array_redis('commodity_classify:0');
            $this->set_array($data);
        }
        $keys = $this->keys_redis($key_index);
        $data = $this->mget_array_redis($keys);
//        $data = $this->sort_classify($data);
        return $this->turn_classify($data);
    }

    /**
     * 方法 : turn_classify
     * @User wuqin
     * @DateTime 2018-03-29 20:39
     * @param $data
     * @return array|bool
     */
    public function turn_classify($data)
    {
        if (empty($data))
            return false;

        //整理数据
        $arm = array();
        $parent_id = 0;
        $children_data = array();
        $result = array();
        foreach ($data as $key => $value) {
            if ($value['parent_id'] == 0) {
                $arm[$value['commodity_classify_id']] = $key;
                $result[$value['commodity_classify_id']] = $value;
                continue;
            }
            if ($parent_id != $value['parent_id']) {
                if ($parent_id == 0) {
                    $parent_id = $value['parent_id'];
                } else {
                    $result[$parent_id]['data'] = $children_data;
                    $children_data = array();
                    $parent_id = $value['parent_id'];
                }
            }

            if ($value['parent_id'] == $parent_id) {
                $children_data[$value['commodity_classify_id']] = $value;
            }

        }
        $result[$parent_id]['data'] = $children_data;
        return $result;
    }

    /**
     * 方法 : get_classify
     * @User wuqin
     * @DateTime 2018-03-14 12:53
     * @param int $parent_id
     * @return mixed
     */
    public function get_classify($parent_id = 0)
    {
        $key_index = 'commodity_classify:*:parent:' . $parent_id;
        if (empty($keys = $this->keys_redis($key_index))) {
            $where = array(
                array(
                    'where', 'parent_id', '=', $parent_id
                )
            );
            $data = $this->get($where, '*', 0, 0);
            if (empty($data))
                $this->set_empty_array_redis('commodity_classify:0:parent:' . $parent_id);
            $this->set_array($data);
        }
        $keys = $this->get_classify_keys_by_parent($parent_id);
        $result = $this->mget_array_redis($keys);
//        $result = $this->sort_classify($result);
        foreach ($result as $key => $value) {
            $data[$value['commodity_classify_id']] = $value;
            unset($result[$key]);
        }
        return $data;
    }

    /**
     * 方法 : get_classify_keys_by_parent
     * @User wuqin
     * @DateTime 2018-03-30 11:19
     * @param $parent_id
     * @return mixed
     */
    public function get_classify_keys_by_parent($parent_id)
    {
        $key_index = "classify_keys_by_parent:" . $parent_id;
        if (empty($keys = $this->exists_redis($key_index))) {
            $this->set_array_redis($key_index, $this->keys_redis('commodity_classify:*:parent:' . $parent_id));
        }
        $data = $this->mget_array_redis($key_index);
        return reset($data);
    }
    /**
     * 方法 : delete_classify
     * @User wuqin
     * @DateTime 2018-03-14 12:53
     * @param $id
     * @return bool
     */
    public function delete_classify($id)
    {
        if (empty($id))
            return false;
        if ($this->where('commodity_classify_id', '=', $id)->delete()) {
            if (!empty($keys = $this->keys_redis('commodity_classify:' . $id . ':*'))) {
                return $this->del_redis($keys);
            }
            return true;
        }
        return false;
    }

    /**
     * 方法 : delete_chidren_classify
     * @User wuqin
     * @DateTime 2018-03-14 12:53
     * @param $parent_id
     * @return bool
     */
    public function delete_chidren_classify($parent_id)
    {
        if (empty($parent_id))
            return false;
        if ($this->where('parent_id', '=', $parent_id)->delete()) {
            if (!empty($keys = $this->keys_redis('commodity_classify:*:parent:' . $parent_id))) {
                return $this->del_redis($keys);
            }
            return true;
        }
        return false;

    }

    /**
     * 方法 : test
     * @User wuqin
     * @DateTime 2018-03-14 12:53
     * @param int $parent_id
     */
    public function test($parent_id = 0)
    {
        $where = ['男装', '女装'];
        DB::enableQueryLog();
        $model = $this;
        for ($i = 0; $i < 2; $i++) {
            $model = $this;
            $model = $model->where('parent_id', '=', $parent_id);
            $model = $model->where('classify_name', '=', $where[$i]);
            p($model->get()->toArray());
            $queries = DB::getQueryLog();
            p($queries);
        }
//         $data;
    }

    /**
     * 方法 : set_empty
     * @User wuqin
     * @DateTime 2018-03-29 20:39
     * @param string $commodity_classify_id
     * @param string $parent_id
     */
    public function set_empty($commodity_classify_id = '0', $parent_id = '0')
    {
        $this->set_empty_array_redis('commodity_classify:' . $commodity_classify_id . ':parent:' . $parent_id);
    }

    /**
     * 方法 : sort_classify
     * @User wuqin
     * @DateTime 2018-03-29 20:39
     * @param $data
     * @return array
     */
    public function sort_classify($data)
    {
        if (empty($data))
            return array();
        //多维排序
        $parent = array();
        foreach ($data as $key => &$value) {
            $value = json_decode($value, true, 512, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
            if (empty($value)) {
                unset($data[$key]);
                continue;
            }
            $parent[] = $value['parent_id'];
        }
        unset($value);
        if (!empty($data))
            array_multisort($parent, SORT_ASC, $data);
        return $data;
    }
}
